﻿#define _CRT_SECURE_NO_WARNINGS 1

class Solution
{
public:
	string convert(string s, int numRows)
	{
		// 处理边界情况
		if (numRows == 1) return s;

		string ret;
		int d = 2 * numRows - 2, n = s.size();

		// 1. 先处理第⼀⾏
		for (int i = 0; i < n; i += d)
			ret += s[i];

		// 2. 处理中间⾏
		for (int k = 1; k < numRows - 1; k++) // 枚举每⼀⾏
		{
			for (int i = k, j = d - k; i < n || j < n; i += d, j += d)
			{
				if (i < n) ret += s[i];
				if (j < n) ret += s[j];
			}
		}

		// 3. 处理最后⼀⾏
		for (int i = numRows - 1; i < n; i += d)
			ret += s[i];
		return ret;
	}
};